<?php require_once "menu.php"; ?>
    <style type="text/css">
        input, select {
            height: 18px;
            font-size: 12px;
        }
    </style>
<table width="100%" class="menubar" cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td class="menudottedline" width="40%"><div class="pathway"></div></td>
        <td class="menudottedline" align="right">
            <table cellpadding="0" cellspacing="0" border="0" id="toolbar">
                <tr height="60" valign="middle" align="center">
                    <td>
                        <a class="toolbar" id="savelink" href="javascript:;">
                        <?php echo $html->image('admin/save_f2.png', array('align' => 'middle', 'border' => 0)); ?><br/>Save
                        </a>
                    </td>
                    <td>
                        <a class="toolbar" id="backlink" href="javascript:;">
                        <?php echo $html->image('admin/restore_f2.png', array('align' => 'middle', 'border' => 0)); ?><br/>Back
                        </a>
                    </td>
                    <td>
                        <a class="toolbar" href="#">
                        <?php echo $html->image('admin/help_f2.png', array('align' => 'middle', 'border' => 0)); ?><br/>Help
                        </a>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<br>
<br>
<div align="center" class="centermain">
    <div class="main">

        <table class="adminheading">
            <tr>
                <th class="info" rowspan="2" nowrap><?php echo $page_title; ?></th>
            </tr>
        </table>

        <form id="restrictionForm" name="restrictionForm" method="post">
            <br/>
    		<div id="error" class="ui-widget" style="display: none;">
    			<div class="ui-state-error ui-corner-all" style="background-color: #fef1ec; padding: 0 .7em;">
    				<p style="text-align: left;">
                        <span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>
    				    <strong>Alert:</strong>
    				    <?php
    				     if (is_array($error)) {
    				        echo "<br/>";
    				        foreach($error as $err){
    		             ?>
    		            <label style="margin-left: 10px;"><?php echo $err; ?></label><br/>
    		            <?php
    				        }
    				     } else {
    		             ?>
    		            <label style="margin-left: 10px;"><?php echo $error; ?></label><br/>
    		            <?php
                         }
                         ?>
                    </p>
    			</div>
    		</div>
			<div id="msg" class="ui-widget" style="display: none;">
				<div class="ui-state-highlight ui-corner-all" style="margin-top: 20px; padding: 0 .7em;">
    				<p style="text-align: left;">
                        <span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>
    				    <strong>Alert:</strong>
    				    <?php
    				     if (is_array($msg)) {
    				        foreach($msg as $err){
    		             ?>
    		            <label style="margin-left: 10px;"><?php echo $err; ?></label><br/>
    		            <?php
    				        }
    				     }
                         ?>
                    </p>
				</div>
			</div>
    		<br/>
            <table class="adminform" cellpadding="0">
                <tr>
                    <th colspan="2">&nbsp;</th>
                </tr>
                <tr>
                    <td width="60%" valign="top" style="padding: 0px !important;">
                        <table class="adminlist" cellpadding=20>
                            <tr>
                                <td colspan="2" class="subheader">
                                    <label>Privilege Information</label>
                                </td>
                            </tr>
                            <tr>
                                <td id="input_label" style="width: 15%"><font color="red"><b>*</b></font>&nbsp;Privilege Name</td>
                                <td style="border: 1px dotted #ccc; background-color: #efefef;">
                                    <input type="text" id="privilege_name" name="data[privilege][name]" value="<?php echo $update['name']; ?>" class="{required:true}" style="width: 505px;" />
                                </td>
                            </tr>
                            <tr>
                                <td id="input_label" style="width: 15%" valign="top">&nbsp;Description</td>
                                <td style="border: 1px dotted #ccc; background-color: #efefef;">
                                    <textarea id="description" name="data[privilege][description]"></textarea>
                                </td>
                            </tr>
                        </table>
                    </td>
                    <td width="60%" valign="top" style="padding: 0px !important;">
                        <table class="adminlist">
                            <tr>
                                <td class="subheader">Restrictions</td>
                            </tr>
                            <tr>
                                <td>
                                    <div id="accordion">
                                    <?php
                                     if ($restrictions) {
                                        foreach($restriction_category as $rc){
                                     ?>
                                    <h3><a href="#"><?php echo $rc['restrictions']['category']?></a></h3>
                                    <div>
                                    <?php
                                            foreach($restrictions as $r){
                                                if ($rc['restrictions']['category'] == $r['Restriction']['category']) {
													if ($grants) {
														foreach ($grants as $g) {
															if ($g['RestrictionGrant']['restrictionId'] == $r['Restriction']['restrictionId']) {
																$checked = "checked='checked'";
																break;
															} else {
																$checked = "";
															}
														}
													} else {
														$checked = "";
													}
                                     ?>
                                    <input type="checkbox" id="rid" name="data[restriction][id][]" value="<?php echo $r['Restriction']['restrictionId']?>" <?php echo $checked; ?> />
                                    <label><?php echo $r['Restriction']['description']?></label><br/>
                                    <?php
                                                }
                                            }
                                     ?>
                                    </div>
                                    <?php
                                        }
                                     }
                                     ?>
                                    </div>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>

            <input type="hidden" id="flag" name="data[flag]" />
        </form>

    </div>
</div>

<script type="text/javascript">
$(document).ready(function() {

	$("#accordion").accordion({
		icons: {
			header: "ui-icon-circle-arrow-e",
			headerSelected: "ui-icon-circle-arrow-s"
		}
    });

    // validate form before submit
    $("#restrictionForm").validate();

    $("#savelink").click(function () {
        var rid = '<?php echo $rid; ?>';
        $("#flag").val('add');
        if (rid == 0) {
            $("#restrictionForm").attr('action', '<?php echo $html->url("/restrictions/restrictiondetails"); ?>');
        } else {
            $("#restrictionForm").attr('action', '<?php echo $html->url("/restrictions/restrictiondetails/".$rid); ?>');
        }

        $("#restrictionForm").submit();
    });

    $("#backlink").click(function () {
        window.location = "<?php echo $html->url('/restrictions/restrictionsmgnt'); ?>";
    });


    var error = '<?php echo $error; ?>';
    var msg = '<?php echo $msg; ?>';
    if (error && !msg) {
        $("#error").show();
        $("#msg").hide();
    }

    if (msg && !error) {
        $("#error").hide();
        $("#msg").show();
    }

    tinyMCE.init({
    	// General options
    	mode : "textareas",
    	theme : "advanced",
    	plugins : "table,inlinepopups",

    	// Theme options
    	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,|,table,removeformat,code",
    	theme_advanced_buttons2 : "",
    	theme_advanced_buttons3 : "",
    	theme_advanced_buttons4 : "",
    	theme_advanced_toolbar_location : "top",
    	theme_advanced_toolbar_align : "left",
    	theme_advanced_statusbar_location : "bottom",
    	theme_advanced_resizing : true,

    	// Example content CSS (should be your site CSS)
    	content_css : "/js/tinymce/examples/css/content.css",

    	// Style formats
    	style_formats : [
    		{title : 'Bold text', inline : 'b'},
    		{title : 'Red text', inline : 'span', styles : {color : '#ff0000'}},
    		{title : 'Red header', block : 'h1', styles : {color : '#ff0000'}},
    		{title : 'Example 1', inline : 'span', classes : 'example1'},
    		{title : 'Example 2', inline : 'span', classes : 'example2'},
    		{title : 'Table styles'},
    		{title : 'Table row 1', selector : 'tr', classes : 'tablerow1'}
    	],

    	formats : {
    		alignleft : {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes : 'left'},
    		aligncenter : {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes : 'center'},
    		alignright : {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes : 'right'},
    		alignfull : {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes : 'full'},
    		bold : {inline : 'span', 'classes' : 'bold'},
    		italic : {inline : 'span', 'classes' : 'italic'},
    		underline : {inline : 'span', 'classes' : 'underline', exact : true},
    		strikethrough : {inline : 'del'},
    		customformat : {inline : 'span', styles : {color : '#00ff00', fontSize : '20px'}, attributes : {title : 'My custom format'}}
    	}
    });

});
</script>